package me.zingon.doc.service.impl;

import me.zingon.doc.dao.UserDao;
import me.zingon.doc.dao.UserProjectDao;
import me.zingon.doc.exception.ServiceException;
import me.zingon.doc.model.User;
import me.zingon.doc.model.UserProject;
import me.zingon.doc.service.ProjectService;
import me.zingon.doc.dao.ProjectDao;
import me.zingon.doc.model.Project;
import me.zingon.doc.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* Created by ZTCGenerator<zingon@aliyun.com> on 2018-5-31.
*/
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectDao projectDao;

    @Autowired
    UserProjectDao userProjectDao;

    @Autowired
    UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insert(Project project){
        return projectDao.insert(project);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int delete(Project project){
        return projectDao.delete(project);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        return  projectDao.deleteByIds(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int update(Project project){
        return projectDao.update(project);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Project getById(Integer id){
        return projectDao.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<Project> list(Project project){
        return projectDao.list(project);
    }

    @Override
    public List<Project> listByUser(User user) {
        return projectDao.listByUser(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertWithUser(Project project, User user) {
        project.setCreateDate(new Date());
        project.setUpdateDate(new Date());
        projectDao.insert(project);

        UserProject up=new UserProject();
        up.setUserId(user.getId());
        up.setProjectId(project.getId());
        up.setPower("crud");
        up.setCreateDate(new Date());
        up.setUpdateDate(new Date());
        userProjectDao.insert(up);
        return 1;
    }

    @Override
    public List<UserVO> listUserByProjectId(Integer projectId) {
        return projectDao.listUserByProjectId(projectId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public UserVO addUser(Integer projectId, String nickname,String power,User user) throws ServiceException {
        if(!power.equals("cru")&&!power.equals("r")){
            throw new ServiceException("user.power.err");
        }
        UserProject loginUp = userProjectDao.list(
                new UserProject()
                        .setUserId(user.getId())
                        .setProjectId(projectId)
                        .setDeleted(false)).get(0);
        if(!loginUp.getPower().contains("d")){
            throw new ServiceException("user.project.nopower");
        }

        List<User> users=userDao.list(new User().setNickname(nickname));
        if(users == null || users.size() == 0){
            throw new ServiceException("user.exists.not");
        }
        List<UserProject> ups=userProjectDao.list(new UserProject().setProjectId(projectId).setUserId(users.get(0).getId()).setDeleted(false));
        if(ups.size()>0){
            throw new ServiceException("user.exists.already");
        }
        UserProject up=new UserProject();
        up.setUserId(users.get(0).getId());
        up.setProjectId(projectId);
        up.setPower(power);
        up.setCreateDate(new Date());
        up.setUpdateDate(new Date());
        userProjectDao.insert(up);
        return new UserVO()
                .setId(up.getId())
                .setAddDate(up.getCreateDate())
                .setName(users.get(0).getNickname())
                .setPower(up.getPower());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteUPByIdWithUser(Integer upId, User user) throws ServiceException {
        UserProject up2del=userProjectDao.getById(upId);
        if(user.getId().equals(up2del.getUserId())){
            throw new ServiceException("user.project.cantdelself");
        }
        UserProject loginUp = userProjectDao.list(
                new UserProject()
                        .setUserId(user.getId())
                        .setProjectId(up2del.getProjectId())
                        .setDeleted(false)).get(0);
        if(!loginUp.getPower().contains("d")){
            throw new ServiceException("user.project.nopower");
        }

        return userProjectDao.update(new UserProject().setId(up2del.getId()).setDeleted(true));
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteProjectByIdWithUser(Integer projectId, User user) throws ServiceException {
        UserProject loginUp = userProjectDao.list(
                new UserProject()
                        .setUserId(user.getId())
                        .setProjectId(projectId)
                        .setDeleted(false)).get(0);
        if(!loginUp.getPower().contains("d")){
            throw new ServiceException("user.project.nopower");
        }
        userProjectDao.updateByProjectId(new UserProject().setProjectId(projectId).setDeleted(true));
        projectDao.update(new Project().setId(projectId).setDeleted(true));
        return 1;
    }
}